import json
import time

import requests


def members(place_id, index, size):
    headers = {
        'orgname': '%E5%91%BC%E5%B8%82%E5%9B%9E%E6%B0%91%E5%8C%BA%E4%B8%87%E8%BE%BE%E5%B9%BF%E5%9C%BA',
        'Accept-Encoding': 'gzip, deflate',
        'plazaCode': '1104483',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7,ja;q=0.6',
        'orgTypeName': '%E5%B9%BF%E5%9C%BA',
        'userid': '259300419157991424',
        'orgcode': '1104483',
        'orgTypeCode': '10003',
        'Connection': 'keep-alive',
        'workingOrgCode': '1104483',
        'areaCode': '10018',
        'groupCode': '101',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
        'tenantId': '2018092600001',
        'Accept': 'application/json, text/plain, */*',
        'username': '%E5%9B%9E%E6%B0%91%E5%8C%BA%E4%B8%87%E8%BE%BE%E5%B9%BF%E5%9C%BA%E7%AE%A1%E7%90%86%E5%91%98',
        'storeCode': '',
        'Referer': 'http://center.beyonds.com/',
        'centerCode': '1008',
        'token': 'Mjc0NDY2MTYwNzU3NDQ4NzA0',
    }

    params = (
        ('scope', place_id),
        ('scopeType', '10003'),
        ('mobileNo', ''),
        ('memberCardCode', ''),
        ('memberCode', ''),
        ('regStartTime', '2018-12-15'),
        ('regEndTime', '2018-12-16'),
        ('expandingChannel', ''),
        ('fromOrgId', ''),
        ('certificateType', ''),
        ('certificateCode', ''),
        ('sex', ''),
        ('name', ''),
        ('activated', ''),
        ('status', ''),
        ('refereeUser', ''),
        ('pageIndex', index),
        ('pageSize', size),
        ('timestr', time.time()),
    )

    response = requests.get('http://center.beyonds.com/member/members', headers=headers, params=params)
    print(response.text)
    return response.text


def write(s):
    f = open("测试.db", "a")
    f.write('%s\n' % s.strip())
    f.close()


def w(s):
    f = open("result.db", "a")
    f.write('%s\n' % s.strip())
    f.close()


if __name__ == '__main__':
    rr = members("1101038", 1, 10)
    rr = members("1000645", 1, 10)
    # f = open("undone.data", "r")
    # while True:
    #     mystr = f.readline()
    #     if not mystr:
    #         break
    #     r = mystr.split(" ")
    #     place_id = r[0]
    #     name = r[1]
    #     rr = json.loads(members(place_id, 1, 10))["data"]["totalCount"]
    #     p = "%s|%s|%s" % (place_id, name, rr)
    #     write(p)
